<?php $this->JQuery()->uienable() ?>
<?php $this->JQuery()->javascriptCaptureStart() ?>

$j(document).ready(function() {
    $j('#form_add').submit(function(e) {
        $j.post(
            '<?php echo $this->geturl('add','category', null, 'admin1')?>',
            {parent:$j('#parent').attr('value'), val:$j('#name').attr('value')}
        );
        return false;
    });

    $j('#form_edit').submit(function(e) {
        $j.post(
            '<?php echo $this->geturl('edit','category', null, 'admin1')?>',
            $j('#form_edit').serialize()
        );
        return false;
    });

    var fixHelper = function(e, ui) {
        ui.children().each(function() {
            $j(this).width($j(this).width());
        });
        return ui;
    };

    $j("#sortable tbody").sortable({
        helper: fixHelper,
        distance: 15,
        stop: function (event, ui) {
            var data = [];
            var pos = <?php echo $this->page>1 ? ($this->page - 1) * $this->per_page :0 ;?>;
            $j("#sortable tbody tr").each(function() {
               data.push({'id':$j(this).attr('rel'), pos:pos++});
            });
            $j.post(
                '<?php echo $this->geturl('position','category', null, 'admin1')?>',
                {data:data}
            );
        }
    }).disableSelection();

    $j('.homepage').click(function() {
        var ele = $j(this);
        if (ele.html() == '0') {
            var act = '1';
        } else {
            var act ='';
        }
        $j.get(
            '<?php echo $this->geturl('homepage','category', null, 'admin1')?>',
            {id: $j(this).attr('rel'), act:act},
            function(data) {
                ele.html(data);
            }
        );
        return false;
    });
});
<?php $this->JQuery()->javascriptCaptureEnd() ?>

<?php $this->headStyle()->captureStart()?>
    #content td { padding:5px}
    #content table { border:1px #aaa solid; padding:5px; width:100%}
    #content th {font-weight:bold;}
    #content .page a {padding:2px;}
<?php $this->headStyle()->captureEnd()?>

<form id="form_add">
    <?php if(!empty($this->category)):?>
        <input type="hidden" name="parent" id="parent" value="<?php echo $this->category->_id?>">
    <?php endif;?>
    <input type="text" name="name" id="name" value="">
    <input type="submit" value="Add">
</form>
<br>
<a href="<?php echo $this->geturl('list', 'category', null, 'admin1')?>">Home</a> >>
<?php if(!empty($this->category)):?>
    <?php foreach($this->category->getParentTree() as $cat):?>
        <a href="<?php echo $this->geturl('list', 'category', null, 'admin1', array('id'=> $cat->_id))?>"><?php echo $cat->name?></a> >>
   <?php endforeach;?>
    <br>
    <form id="form_edit">
        <input type="hidden" name="edit_id" id="edit_id" value="<?php echo $this->category->_id?>">
        <table border="1">
            <tr>
                <td colspan="2">
                    <?php if(empty($this->category->homepage)):?>
                        <a href="<?php echo $this->geturl('homepage', 'category', null, 'admin1', array('id'=> $this->category->_id, 'act' => 1, 'back' => $_GET['id']))?>">Display on homepage</a>
                    <?php else: ?>
                        <a href="<?php echo $this->geturl('homepage', 'category', null, 'admin1', array('id'=> $this->category->_id, 'act' => 0, 'back' => $_GET['id']))?>">Hide from homepage</a>
                    <?php endif;?>
                </td>
            </tr>
            <tr>
                <th>Name</th>
                <td>
                    English:<input type="text" name="name_en" id="name_edit_en" value="<?php echo $this->category->nm_en?>"><br>
                    SC: <input type="text" name="name_sc" id="name_edit_sc" value="<?php echo $this->category->nm_sc?>">
                </td>
            </tr>
            <tr>
                <th>Navigation bar name</th>
                <td>
                    English:<input type="text" name="nav_name_en" id="name_edit_en" value="<?php echo $this->category->nav_nm_en?>"><br>
                    SC: <input type="text" name="nav_name_sc" id="name_edit_sc" value="<?php echo $this->category->nav_nm_sc?>">
                </td>
            </tr>            
            <tr>
                <th>Special handle (id)</th>
                <td><input type="text" name="handle" id="handle_edit" value="<?php echo $this->category->id?>"></td>
            </tr>
            <tr>
                <th>Status</th>
                <td><?php echo $this->category->status?></td>
            </tr>
            <tr>
                <th>URL</th>
                <td>
                    PinYin: <input type="text" name="url_sc" id="url_sc_edit" value="<?php echo $this->category->url_sc?>">
                </td>
            </tr>
            <tr>
                <th>Sold/Expired text</th>
                <td>
                    English:<input type="text" name="sold_en" id="so_en_edit" value="<?php echo $this->category->sold_en?>"><br>
                    SC:<input type="text" name="sold_sc" id="so_sc_edit" value="<?php echo $this->category->sold_sc?>">
                </td>
            </tr>
            <tr>
                <th>Page title</th>
                <td>
                    English:<input type="text" name="page_title_en" id="page_title_edit_en" value="<?php echo $this->category->page_title_en?>"><br>
                    SC: <input type="text" name="page_title_sc" id="page_title_edit_sc" value="<?php echo $this->category->page_title_sc?>">
                </td>
            </tr>
            <tr>
                <th>Meta Description</th>
                <td>
                    English:<input type="text" name="description_en" id="description_edit_en" value="<?php echo $this->category->description_en?>"><br>
                    SC: <input type="text" name="description_sc" id="description_edit_sc" value="<?php echo $this->category->description_sc?>">
                </td>
            </tr>
            <tr>
                <th>Blurb</th>
                <td>
                    English:<textarea style="width:100%; height:200px" name="blurb_en" id="blurb_edit_en"><?php echo $this->category->blurb_en?></textarea><br>
                    SC: <textarea  style="width:100%; height:200px" name="blurb_sc" id="blurb_edit_sc"><?php echo $this->category->blurb_sc?></textarea>
                </td>
            </tr>                
            <tr>
                <th>Display on homepage</th>
                <td>
                    <input type="radio" name="homepage" id="homepage_edit_off" value="0" <?php echo $this->category->homepage!=1 ? 'checked' : ''?>><label for="homepage_edit">Off</label>
                    <input type="radio" name="homepage" id="homepage_edit_on" value="1" <?php echo $this->category->homepage==1 ? 'checked' : ''?>><label for="homepage_edit">On</label>
                </td>
            </tr>
            <tr>
                <th>Post an ad</th>
                <td>
                    <input type="radio" name="post_an_ad" id="post_an_ad_edit_off" value="0" <?php echo $this->category->post_an_ad!=1 ? 'checked' : ''?>><label for="post_an_ad_edit">Off</label>
                    <input type="radio" name="post_an_ad" id="post_an_ad_edit_on" value="1" <?php echo $this->category->post_an_ad==1 ? 'checked' : ''?>><label for="post_an_ad_edit">On</label>
                </td>
            </tr>
            <tr>
                <th>Category Search list</th>
                <td>
                    <input type="radio" name="category_search_list" id="category_search_list_edit_off" value="0" <?php echo $this->category->category_search_list!=1 ? 'checked' : ''?>><label for="category_search_list_edit">Off</label>
                    <input type="radio" name="category_search_list" id="category_search_list_edit_on" value="1" <?php echo $this->category->category_search_list==1 ? 'checked' : ''?>><label for="category_search_list_edit">On</label>
                </td>
            </tr>
            <tr>
                <th>Display in category list (not post an ad)</th>
                <td>
                    <input type="radio" name="display_website" id="display_website_edit_off" value="0" <?php echo $this->category->display_website!=1 ? 'checked' : ''?>><label for="display_website_edit">Off</label>
                    <input type="radio" name="display_website" id="display_website_edit_on" value="1" <?php echo $this->category->display_website==1 ? 'checked' : ''?>><label for="display_website_edit">On</label>
                </td>
            </tr>
            <tr>
                <th>Customization</th>
                <td>
                    <select name="customization">
                        <option value="">No customization</option>
                    <?php                        
                        $dir_iterator = new RecursiveDirectoryIterator(APPLICATION_PATH .'/models/Listing');
                        $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::CATCH_GET_CHILD);

                        foreach ($iterator as $file) {
                            $filename  = str_replace('.php','', basename($file));
                            if (!is_dir($file) && !in_array($filename, array('Attribute', 'Interface','Regular'))) {
                                if ($this->category->customization == strtolower($filename)) {
                                    echo '<option selected="selected" value="'.strtolower($filename).'">'.$filename.'</option>';
                                }
                                else {
                                    echo '<option value="'.strtolower($filename).'">'.$filename.'</option>';
                                }
                            }
                        }
                    ?>
                    </select>
                </td>
            </tr>
            <tr>
                <th>Position big list</th>
                <td>
                    <input type="text" name="pos_big_list"  value="<?php echo (int) $this->category->pos_big_list?>">
                </td>
            </tr>
        </table>
        <input type="submit" value="Validate">
    </form>

    <?php endif;?>
<table border="1" id="sortable">
<?php
    $_GET['id'] = !empty($_GET['id']) ? $_GET['id']: '';
    $get = $_GET; unset($get['page']);
    $pagination = $this->pagination($this->listing_number, $this->page, $this->per_page, 4, $this->geturl('list', 'category', null, 'admin1', $get));
?>
<?php if($this->listing_number == 0):?>
	No Categories are defined
<?php else:?>
    <thead>
        <tr>
            <th>

            </th>
            <th>
                Name
            </th>
            <th>
                <a href="<?php echo $this->geturl('list', 'category', null, 'admin1', array('sort' => 'recent', 'page' => $this->page))?>">Creation date</a>
            </th>
            <th>
                <a href="<?php echo $this->geturl('list', 'category', null, 'admin1', array('sort' => 'status', 'page' => $this->page))?>">Status</a>
            </th>
            <th>Homepage</th>
            <th>Category Search list</th>
            <th>Post an ad</th>
            <th>Display in category list (not post an ad)</th>
            <th>Customization</th>
            <th>Position nav</th>
            <th>Position big list</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($this->list as $item):?>
        <tr class="ui-state-default" rel="<?php echo $item->_id?>">
            <td>
                <?php if($item->status == Model_Category::STATUS_DELETED):?>
                    <a href="<?php echo $this->geturl('activate', 'category', null, 'admin1', array('id'=> $item->_id, 'back' => $_GET['id']))?>">Activate</a>
                <?php else: ?>
                    <a href="<?php echo $this->geturl('delete', 'category', null, 'admin1', array('id'=> $item->_id, 'back' => $_GET['id']))?>">Delete</a>
                <?php endif;?>
            </td>
            <td>
                <a href="<?php echo $this->geturl('list', 'category', null, 'admin1', array('id'=> $item->_id, 'back' => $_GET['id']))?>">
                    <?php echo htmlentities((string)$item->name_en, ENT_COMPAT, 'utf-8'); ?>
                </a>
            </td>
            <td>
                <?php echo date ('Y-m-d H:i', strtotime($item->created_at))?>
            </td>
            <td>
                <?php echo $item->status?>
            </td>
            <td>
                <span rel="<?php echo $item->_id?>" class="homepage"><?php echo (int)$item->homepage?></span>
            </td>
            <td>
                <?php echo $item->category_search_list?>
            </td>
            <td>
                <?php echo $item->post_an_ad?>
            </td>
            <td>
                <?php echo $item->display_website?>
            </td>
            <td>
                <?php echo $item->customization?>
            </td>
            <td><?php echo $item->pos?></td>
            <td><?php echo $item->pos_big_list?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
<?php endif;?>
    
</table>
<?php echo $pagination;?>
